//
//  ZZTabViewController.m
//  ZZLC
//
//  Created by 杨春至 on 2017/12/13.
//  Copyright © 2017年 ZhongZeng. All rights reserved.
//

#import "ZZTabViewController.h"
#import "RecommendVC.h"
#import "MeVC.h"
#import "MMMVC.h"
#import "ZZBaseNavigationController.h"
#import "UIView+AxcShaking.h"

@interface ZZTabViewController (){
    NSMutableArray * _tabBarButtonArray;
}
@end

@implementation ZZTabViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configSubView];
}
- (void)configSubView{
    RecommendVC *rVC = [[RecommendVC alloc]init];
    MMMVC *moneyVC = [[MMMVC alloc]init];
    moneyVC.title = @"理财产品";
    MeVC *mVC = [[MeVC alloc]init];
    ZZBaseNavigationController *rNaVC  = [self fillVC:rVC withTitle:@"推荐" SelectImageName:@"recommenSelect" DisSelectedName:@"recommenDisSelect"];
    ZZBaseNavigationController *mmNaVC  = [self fillVC:moneyVC withTitle:@"理财" SelectImageName:@"mmmSelect" DisSelectedName:@"mmmDisSelect"];
    ZZBaseNavigationController *meNaVC  = [self fillVC:mVC withTitle:@"我的" SelectImageName:@"meSelect" DisSelectedName:@"meDisSelect"];
    self.tabBar.translucent = NO;
    self.viewControllers = @[rNaVC,mmNaVC,meNaVC];
    if (!_tabBarButtonArray) {
        _tabBarButtonArray = [NSMutableArray array];
        for (UIView *tabBarButton in self.tabBar.subviews) {
            if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
                [_tabBarButtonArray addObject:tabBarButton];
            }
        }
    }
    
}
- (ZZBaseNavigationController *)fillVC:(UIViewController *)VC withTitle:(NSString *)title SelectImageName:(NSString *)seleName DisSelectedName:(NSString *)disSeleName{
    ZZBaseNavigationController *naVC = [[ZZBaseNavigationController alloc]initWithRootViewController:VC];
    UITabBarItem * BarItem = [self tabBarItemWithTitle:title image:UIImageMake(disSeleName) selectedImage: UIImageMake(seleName) tag:0];
    naVC.tabBarItem  = BarItem;
    return naVC;
}
- (UITabBarItem *)tabBarItemWithTitle:(NSString *)title image:(UIImage *)image selectedImage:(UIImage *)selectedImage tag:(NSInteger)tag{
    UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:image tag:tag];
    tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:ZZGoldColor} forState:UIControlStateSelected];
    tabBarItem.qmui_doubleTapBlock = ^(UITabBarItem *tabBarItem, NSInteger index) {
        
    };
    return tabBarItem;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    [self animationWithIndex:[self.tabBar.items indexOfObject:item]];
}
#pragma mark - TabBar Item选中时的动画
- (void)animationWithIndex:(NSInteger) index {

    CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    animation.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.duration =0.1;
    animation.repeatCount = 1;
    animation.autoreverses = YES;
    animation.fromValue = @0.6;
    animation.toValue = @1.2;
    [[_tabBarButtonArray[index] layer]addAnimation:animation forKey:nil];
    
}
@end
